//2009/08/27 23:45:16
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class MinDifference
{
public:
    int closestElements(int A0, int X, int Y, int M, int n)
    {
		vector<int> v;
		v.push_back(A0);
		cout << A0;
		for(int i=1; i<n; i++)
		{
			int one = (v[i-1] * X + Y ) % M;
			cout << ", " << one;
			v.push_back(one);
		}
		cout << endl;
		sort(v.begin(), v.end());
		int mmin = -1;
		for(int i=0; i<v.size() - 1; i++)
		{
			if(mmin > abs(v[i+1] -v[i]) || mmin == -1)
				mmin = abs(v[i+1] - v[i]);
		}
		return mmin;
    }
};
